import React, { Component } from "react";
import QueueAnim from "rc-queue-anim";
import { message, Table } from "antd";
import TagSelect from "@/components/TagSelect/TagSelect";
import PushedTab from "@/components/DetailTabs/PushedTab";
import { connect } from "dva";
import CustomModal from "@/components/CustomModal/index";
import { pushSiteList } from "../../services/services";
import { judgeMonCode, handleMonTextForNewsList } from '@/utils/formatMonTextStyle.js'

@connect(({ push }) => ({ push }))
class PushedR extends Component {
  constructor(props) {
    super(props);
    this.state = {
      pageSize: 10, //表格数据当前页显示条数
      currentPage: 1, //表格数据当前页码
      category: "文章", //分类名称
      categoryId: 1, //分类id
      keyword: "", //搜索关键词
      create_time: "1970-01-01 00:00:00", //初始查询全部时间
      searchType: 1, //搜索类型
      tableLoading: false, //表格加载效果
      showDetailVariable: true, //显示详情页变量
      tabKey: "1", //详情tab页索引
      timer_info: "全部",
      customModalVisible: false,
      pushSiteList: [],
      id:'',
    };
  }
  componentDidMount() {
    this.getTableData();
    console.log(this.props);
  }
  // 获取表格数据
  getTableData = async () => {
    this.setState({
      tableLoading: true
    });
    const { categoryId, currentPage, searchType, pageSize, create_time, keyword } = this.state;
    const result = await this.props.dispatch({
      type: "push/getPushedDraftList",
      payload: {
        content_type: categoryId,
        create_time: create_time,
        search_type: searchType,
        keyword: keyword,
        page: currentPage,
        per_page: pageSize,
        level: 0
      }
    });
    if (result) {
      this.setState({
        tableLoading: false
      });
    }
  };
  // 类别切换
  categoryChange = data => {
    this.setState(
      {
        tableLoading: true,
        keyword: "",
        category: data.category_name,
        categoryId: data.category_id
      },
      () => {
        this.getTableData();
      }
    );
  };
  // 时间选择切换
  selectedTimeChange = (timer, doc) => {
    this.setState(
      {
        tableLoading: true,
        create_time: timer,
        timer_info: doc
      },
      () => {
        this.getTableData();
      }
    );
  };
  // 搜索
  handleSeachButton = data => {
    this.setState(
      {
        tableLoading: true,
        categoryId: data.category_id,
        keyword: data.searchKeyword,
        searchType: data.searchType,
        category: data.category_name
      },
      () => {
        this.getTableData();
      }
    );
  };

  // 跳转值推送数据
  showPushedData = row => {
    this.setState({
      showDetailVariable: false,
      tabKey: "1",
      id: row.id
    });
    // 推稿数据
    this.props.dispatch({
      type: "push/getPushedDraftData",
      payload: {
        id: row.id,
        page: 1,
        per_page: 1000
      }
    });
    // 内容
    this.props.dispatch({
      type: "push/getContentDetailsOther",
      payload: {
        id: row.id
      }
    });
  };
  // 跳转至详情
  showDetailChange = row => {
    this.setState({
      showDetailVariable: false,
      tabKey: "2",
      id: row.id
    });
    // 推稿数据
    this.props.dispatch({
      type: "push/getPushedDraftData",
      payload: {
        id: row.id,
        page: 1,
        per_page: 1000
      }
    });
    // 统计数据
    this.props.dispatch({
      type: "push/getDetailesStatisticalData",
      payload: {
        site_id: row.site_id,
        content_type: row.content_type,
        content_id: row.content_id,
        source: 0
      }
    });
    // 内容
    this.props.dispatch({
      type: "push/getContentDetailsOther",
      payload: {
        id: row.id
      }
    });
  };
  // 表格显示条数切换
  pageSizeChange = (current, size) => {
    this.setState(
      {
        pageSize: size,
        currentPage: current
      },
      () => {
        this.getTableData();
      }
    );
  };
  // 当前页面变化
  currentPageChange = (page, pageSize) => {
    this.setState(
      {
        currentPage: page
      },
      () => {
        this.getTableData();
      }
    );
  };
  // 返回上一级
  goBack = () => {
    this.setState({
      showDetailVariable: true
    });
  };
  showCustomModal = record => {
    const receive_sites = record.receive_site;
    if (!receive_sites) {
      return message.warn("暂无已推送站点");
    }
    const params = {
      id: receive_sites.join(",")
    };
    pushSiteList(params)
      .then(res => {
        if (res.code === 200) {
          this.setState({
            customModalVisible: true,
            pushSiteList: res.data
          });
        } else {
          message.error("请求失败");
        }
      })
      .catch(err => {
        console.log(err);
      });
  };
  handleCustomModalOk = () => {
    this.setState({
      customModalVisible: false
    });
  };
  handleCustomModalCancel = () => {
    this.setState({
      customModalVisible: false
    });
  };
  render() {
    const { tableLoading } = this.state;
    const { pushedData, pushedTotal, detail, statistiData, pushedTableData,pushedTableLoading} = this.props.push;
    // 表格表头
    const columns = [
      {
        title: "标题",
        dataIndex: "title",
        // key: "title",
        align: "center",
        render: (text, record, index) => (
          <div style={{ textAlign: "left" }}>
            {/* 判断是否符合蒙文编码 */}
            { judgeMonCode(text) ?
              handleMonTextForNewsList(pushedData[index].create_time, text)
            :
            <div style={{ textAlign: "center" }}> {text} </div> 
            }
          </div>
        )
      },
      {
        title: "类型",
        dataIndex: "content_type",
        key: "content_type",
        align: "center",
        render: (text, record) => {
          if (text === 1) {
            return "文章";
          } else if (text === 2) {
            return "图集";
          } else if (text === 4) {
            return "视频";
          } else {
            return "音频";
          }
        }
      },
      {
        title: "发布站点",
        dataIndex: "site_name",
        key: "site_name",
        align: "center"
      },
      {
        title: "发布人",
        dataIndex: "create_user",
        // key: "create_user",
        align: "center",
        render: (create_user, record, index) => (
          <div>
            {/* 判断是否符合蒙文编码 */}
            { judgeMonCode(create_user) ?
              handleMonTextForNewsList(pushedData[index].create_time, create_user)
            :
              <span> {create_user} </span> 
            }
          </div>
        )
      },
      {
        title: "推送时间",
        dataIndex: "create_time",
        key: "create_time",
        align: "center"
      },
      {
        title: "操作",
        key: "action",
        align: "center",
        render: (text, record) => (
          <div>
            <span style={{ cursor: "pointer", color: "#1890ff" }} onClick={this.showPushedData.bind(this, record)}>
              推送数据&nbsp;&nbsp;
            </span>
            <span style={{ cursor: "pointer", color: "#1890ff" }} onClick={this.showDetailChange.bind(this, record)}>
              查看详情&nbsp;&nbsp;
            </span>
            {/* <span style={{ cursor: "pointer", color: "#1890ff" }} onClick={this.showCustomModal.bind(this, record)}>
              推送站点
            </span> */}
          </div>
        )
      }
    ];
    // 表格分页器配置项
    const paginationProps = {
      current: this.state.currentPage,
      showSizeChanger: true,
      showQuickJumper: true,
      total: pushedTotal,
      showTotal: () => `共 ${pushedTotal} 条记录`,
      pageSize: this.state.pageSize,
      pageSizeOptions: ["5", "10", "20", "50"],
      onChange: this.currentPageChange,
      onShowSizeChange: this.pageSizeChange
    };
    return (
      <QueueAnim className="PushedR">
        <div key="t1" className="BasicContent">
          <div className="main" style={{ padding: 40 }}>
            {this.state.showDetailVariable ? (
              <div>
                <TagSelect
                  currentCategory={[this.state.category]}
                  currentTimer={[this.state.timer_info]}
                  showAllDelVariable={false}
                  getcategoryChange={this.categoryChange}
                  getSelectedTimeChange={this.selectedTimeChange}
                  handleSeachButton={this.handleSeachButton}
                />
                <Table loading={tableLoading} rowKey={recored => recored.id} columns={columns} dataSource={pushedData} bordered pagination={paginationProps} />
              </div>
            ) : (
              <PushedTab id={this.state.id} Loading={pushedTableLoading} statisticalData={statistiData} parentBack={this.goBack} TableData={pushedTableData} contentData={detail} tabKey={this.state.tabKey} />
            )}
          </div>
        </div>
        <CustomModal
          customModalVisible={this.state.customModalVisible}
          handleCustomModalOk={this.handleCustomModalOk}
          handleCustomModalCancel={this.handleCustomModalCancel}
          data={this.state.pushSiteList}
        />
      </QueueAnim>
    );
  }
}

export default PushedR;
